package com.samphanie.dries.security.userdetails;

import com.samphanie.dries.repository.UserRepo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.stereotype.Service;

/**
 * @Description: 密码升级
 * @Author ZSY
 * @createTime 2021/2/26 11:47
 */
@RequiredArgsConstructor
@Service
public class UserDetailsPasswordServiceImpl implements UserDetailsPasswordService {

    private final UserRepo userRepo;

    @Override
    public UserDetails updatePassword(UserDetails userDetails, String newPassword) {
        return userRepo.findOptionalByUsername(userDetails.getUsername())
                .map(user -> (UserDetails) userRepo.save(user.withPassword(newPassword)))
                .orElse(userDetails);
    }
}
